package com.zhdl.modules.prefiresys.service.impl;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.project.common.service.impl.BaseServiceImpl;
import com.querydsl.core.BooleanBuilder;
import com.querydsl.core.types.Projections;
import com.querydsl.jpa.impl.JPAQuery;
import com.zhdl.modules.prefiresys.entity.DetectorBlowBackLog;
import com.zhdl.modules.prefiresys.entity.QDetector;
import com.zhdl.modules.prefiresys.entity.QDetectorBlowBackLog;
import com.zhdl.modules.prefiresys.repository.DetectorBlowBackLogRepository;
import com.zhdl.modules.prefiresys.service.DetectorBlowBackLogService;
import com.zhdl.modules.prefiresys.web.request.DetectorBlowBackLogPageRequest;
import com.zhdl.modules.prefiresys.web.response.DetectorBlowBackLogPageQueryResult;
import com.zhdl.modules.prefiresys.web.response.DetectorBlowBackLogPageResp;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class DetectorBlowBackLogServiceImpl extends BaseServiceImpl<String, DetectorBlowBackLog, DetectorBlowBackLogRepository> implements DetectorBlowBackLogService {
    /**
     * 分页查询 探测器反吹记录
     *
     * @param request
     * @param pageable
     * @return
     */
    @Override
    public Page<DetectorBlowBackLogPageResp> findAllRecord(DetectorBlowBackLogPageRequest request, Pageable pageable) {
        QDetectorBlowBackLog qDetectorBlowBackLog = QDetectorBlowBackLog.detectorBlowBackLog;
        QDetector qDetector = QDetector.detector;
        JPAQuery<DetectorBlowBackLogPageQueryResult> query = jpaQueryFactory()
                .select(Projections.constructor(DetectorBlowBackLogPageQueryResult.class,
                        qDetectorBlowBackLog.id,
                        qDetectorBlowBackLog.startTime,
                        qDetectorBlowBackLog.endTime,
                        qDetectorBlowBackLog.result,
                        qDetector.detectorName.as("detectorName")))
                .from(qDetectorBlowBackLog);
        query.leftJoin(qDetector).on(qDetector.id.eq(qDetectorBlowBackLog.detectorId));
        BooleanBuilder builder = new BooleanBuilder();
        builder.and(qDetectorBlowBackLog.startTime.between(request.getStartTime(), request.getEndTime()));

        if (request.getDetectorId().size() > 0) {
            BooleanBuilder builder1 = new BooleanBuilder();
            for (String id : request.getDetectorId())
                builder1.or(qDetectorBlowBackLog.detectorId.eq(id));

            builder.and(builder1);
        }

        List<DetectorBlowBackLogPageQueryResult> fetch = query.where(builder)
                .orderBy(qDetectorBlowBackLog.createDate.desc())
                .offset(pageable.getOffset())
                .limit(pageable.getPageSize())
                .fetch();
        //long total = query.fetch().size();
        long total = jpaQueryFactory().select(qDetectorBlowBackLog.count()).from(qDetectorBlowBackLog).
                where(builder).fetchOne();

        List<DetectorBlowBackLogPageResp> responses = new ArrayList<>();
        fetch.forEach(item -> {
            var response = new DetectorBlowBackLogPageResp();
            BeanUtil.copyProperties(item, response, CopyOptions.create().setIgnoreNullValue(true));
            response.setResult(item.getResult() == 0 ? "未完成" : "已完成");
            responses.add(response);
        });
        return new PageImpl<>(responses, pageable, total);
    }

}
